package com.cnfangmao.masterdata.controller;


import java.util.List;

import javax.validation.constraints.NotNull;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cnfangmao.masterdata.common.response.ResBody;
import com.cnfangmao.masterdata.common.response.ResCodeEnum;
import com.cnfangmao.masterdata.dto.GeneralSearchDto;
import com.cnfangmao.masterdata.dto.RoleDto;
import com.cnfangmao.masterdata.dto.RoleSearchDto;
import com.cnfangmao.masterdata.model.Role;
import com.cnfangmao.masterdata.service.RoleService;
import com.cnfangmao.masterdata.vo.GeneralPageVo;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author chenxd
 * @since 2020-04-17
 */
@RequestMapping("/role")
@RestController()
@Api(tags = "0-3、配置-角色管理")
public class RoleController {
	
	private static final Logger logger =LoggerFactory.getLogger(RoleController.class);
	
	@Autowired
	private RoleService roleService;
	
	 /**
     * 获取角色列表
     */
    @RequestMapping(value="/list.do", method = RequestMethod.POST)
    @ApiOperation(value="角色列表", notes = "")
    @ApiImplicitParam(name = "dto", value = "角色列表查询条件", required = true, paramType = "body", dataType = "GeneralSearchDto«RoleSearchDto»")
    public ResBody<GeneralPageVo<Role>> listRole(@RequestBody GeneralSearchDto<RoleSearchDto> dto)
    {
    	ResBody<GeneralPageVo<Role>> resBody =new ResBody<GeneralPageVo<Role>>();
    	try {
        	IPage<Role> data =roleService.queryListRole(dto);
        	GeneralPageVo<Role> result= new GeneralPageVo<Role>();
			result.setPageNo(data.getCurrent());
			result.setPageSize(data.getPages());
			result.setRecords(data.getRecords());
			result.setTotalCount(data.getTotal());
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(result);
			resBody.message("查询成功");
		} catch (Exception e) {
			logger.error(String.format("run list error in RoleController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
    	return resBody;
    }
    
    /**
     * 创建角色
     */
    @RequestMapping(value="/create.do", method = RequestMethod.POST)
    @ApiOperation(value="创建角色", notes = "")
    @ApiImplicitParam(name = "dto", value = "创建角色",required = true, paramType = "body", dataType = "RoleDto")
    public ResBody<Boolean> createRole(@Validated @RequestBody  @NotNull RoleDto dto){
    	ResBody<Boolean> resBody = new ResBody<Boolean>();
    	try {
    		if(dto.getRoleName().isEmpty())
        	{
        		resBody.code(ResCodeEnum.PARAM_NULL.getCode());
            	resBody.message("角色名为空");
                return resBody;
        	}
        	if(roleService.findByRoleName(dto.getRoleName())!=null)
        	{
        		resBody.code(ResCodeEnum.PARAM_NULL.getCode());
            	resBody.message("角色名已经存在");
                return resBody;
        	}
        	roleService.save(dto, "");
        	resBody.code(ResCodeEnum.SUCCESS.getCode());
            resBody.data(true);
            resBody.message("创建成功");
		} catch (Exception e) {
			logger.error(String.format("run create error in RoleController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
    	
    	return resBody;
    }
    
    /**
     * 更新角色
     */
    @RequestMapping(value="/update.do", method = RequestMethod.POST)
    @ApiOperation(value="更新角色", notes = "")
    @ApiImplicitParam(name = "dto", value = "更新角色信息",required = true, paramType = "body", dataType = "RoleDto")
    public ResBody<Boolean> updateRole(@Validated @RequestBody  @NotNull RoleDto dto){
    	ResBody<Boolean> resBody = new ResBody<Boolean>();
    	try {
    		if(dto.getRoleName().isEmpty())
        	{
        		resBody.code(ResCodeEnum.PARAM_NULL.getCode());
            	resBody.message("角色名为空");
                return resBody;
        	}
        	Role role=roleService.findByRoleName(dto.getRoleName());
        	if(role!=null&&!role.getRoleId().equals(dto.getRoleId()))
    		{
        		resBody.code(ResCodeEnum.PARAM_NULL.getCode());
            	resBody.message("角色名已经存在");
                return resBody;
    		}
        	roleService.update(dto, "");
        	resBody.code(ResCodeEnum.SUCCESS.getCode());
            resBody.data(true);
            resBody.message("修改成功");
		} catch (Exception e) {
			logger.error(String.format("run update error in RoleController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
    	return resBody;
    }

    /**
     * 删除角色
     */
    @RequestMapping(value="/delete.do", method = RequestMethod.POST)
    @ApiOperation(value="删除角色", notes = "")
    @ApiImplicitParam(name = "roleId", value = "角色roleId", required = true, dataType = "string")
    public ResBody<Boolean> deleteRole(@Validated @RequestBody @NotNull String roleId){
    	ResBody<Boolean> resBody = new ResBody<Boolean>();
    	try {
        	roleService.delete(roleId);
        	resBody.code(ResCodeEnum.SUCCESS.getCode());
            resBody.data(true);
            resBody.message("删除成功");
		} catch (Exception e) {
			logger.error(String.format("run delete error in RoleController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
    	return resBody;
    }
    
    /**
     * 获取角色详情
     */
    @RequestMapping(value="/getRole.do", method = RequestMethod.POST)
    @ApiOperation(value="获取角色详情", notes = "")
    @ApiImplicitParam(name = "roleId", value = "角色roleId", required = true, dataType = "string")
    public ResBody<Role> getRole(@Validated @RequestBody @NotNull String roleId){
    	ResBody<Role> resBody = new ResBody<Role>();
    	try {
        	Role role=roleService.getById(roleId);
        	resBody.code(ResCodeEnum.SUCCESS.getCode());
    		resBody.data(role);
		} catch (Exception e) {
			logger.error(String.format("run getRole error in RoleController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
    	return resBody;
    }    

    /**
     * 获取所有角色
     */
    @RequestMapping(value="/getAllRoles.do", method = RequestMethod.GET)
    @ApiOperation(value="获取所有角色", notes = "")
    public ResBody<List<Role>> getAllRoles(){
    	ResBody<List<Role>> resBody = new ResBody<List<Role>>();
    	try {
        	List<Role> roles =roleService.getAllRoles();
        	resBody.code(ResCodeEnum.SUCCESS.getCode());
    		resBody.data(roles);
		} catch (Exception e) {
			logger.error(String.format("run getAllRoles error in RoleController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
    	return resBody;
    }

}

